package com.eshop.modules.quartz.rest;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.slf4j.LoggerFactory;
import java.lang.invoke.SerializedLambda;
import org.springframework.web.bind.annotation.DeleteMapping;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.modules.aop.ForbidSubmit;
import com.eshop.exception.BadRequestException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.quartz.domain.QuartzJob;
import com.eshop.modules.quartz.service.dto.QuartzLogDto;
import com.eshop.modules.quartz.service.dto.QuartzLogQueryCriteria;
import java.io.IOException;
import java.util.List;
import com.eshop.modules.quartz.service.dto.QuartzJobDto;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.quartz.service.dto.QuartzJobQueryCriteria;
import com.eshop.modules.quartz.service.QuartzLogService;
import com.eshop.dozer.service.IGenerator;
import com.eshop.modules.quartz.service.QuartzJobService;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = { "系统:定时任务管理" })
@RequestMapping({ "/api/jobs" })
public class QuartzJobController
{
    private static final Logger log;
    private static final String ENTITY_NAME = "quartzJob";
    private final QuartzJobService quartzJobService;
    private final IGenerator generator;
    private final QuartzLogService quartzLogService;
    
    public QuartzJobController(final QuartzJobService quartzJobService, final IGenerator generator, final QuartzLogService quartzLogService) {
        this.quartzJobService = quartzJobService;
        this.generator = generator;
        this.quartzLogService = quartzLogService;
    }
    
    @Log("查询定时任务")
    @ApiOperation("查询定时任务")
    @GetMapping
    @PreAuthorize("@el.check('admin','timing:list')")
    public ResponseEntity<Object> getJobs(final QuartzJobQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.quartzJobService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @Log("导出任务数据")
    @ApiOperation("导出任务数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('admin','timing:list')")
    public void download(final HttpServletResponse response, final QuartzJobQueryCriteria criteria) throws IOException {
        this.quartzJobService.download(this.generator.convert((List)this.quartzJobService.queryAll(criteria), (Class)QuartzJobDto.class), response);
    }
    
    @Log("导出日志数据")
    @ApiOperation("导出日志数据")
    @GetMapping({ "/logs/download" })
    @PreAuthorize("@el.check('admin','timing:list')")
    public void downloadLog(final HttpServletResponse response, final QuartzLogQueryCriteria criteria) throws IOException {
        this.quartzLogService.download(this.generator.convert((List)this.quartzLogService.queryAll(criteria), (Class)QuartzLogDto.class), response);
    }
    
    @ApiOperation("查询任务执行日志")
    @GetMapping({ "/logs" })
    @PreAuthorize("@el.check('admin','timing:list')")
    public ResponseEntity<Object> getJobLogs(final QuartzLogQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.quartzLogService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @ForbidSubmit
    @Log("新增定时任务")
    @ApiOperation("新增定时任务")
    @PostMapping
    @PreAuthorize("@el.check('admin','timing:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody final QuartzJob resources) {
        if (resources.getId() != null) {
            throw new BadRequestException("A new quartzJob cannot already have an ID");
        }
        return new ResponseEntity(this.quartzJobService.save(resources), HttpStatus.CREATED);
    }
    
    @ForbidSubmit
    @Log("修改定时任务")
    @ApiOperation("修改定时任务")
    @PutMapping
    @PreAuthorize("@el.check('admin','timing:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody final QuartzJob resources) {
        this.quartzJobService.updateById(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("更改定时任务状态")
    @ApiOperation("更改定时任务状态")
    @PutMapping({ "/{id}" })
    @PreAuthorize("@el.check('admin','timing:edit')")
    public ResponseEntity<Object> updateIsPause(@PathVariable final Long id) {
        this.quartzJobService.updateIsPause((QuartzJob)this.quartzJobService.getOne(Wrappers.<QuartzJob>lambdaQuery().eq(QuartzJob::getId, id)));
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("执行定时任务")
    @ApiOperation("执行定时任务")
    @PutMapping({ "/exec/{id}" })
    @PreAuthorize("@el.check('admin','timing:edit')")
    public ResponseEntity<Object> execution(@PathVariable final Long id) {
        this.quartzJobService.execution((QuartzJob)this.quartzJobService.getOne(Wrappers.<QuartzJob>lambdaQuery().eq(QuartzJob::getId, id)));
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除定时任务")
    @ApiOperation("删除定时任务")
    @DeleteMapping
    @PreAuthorize("@el.check('admin','timing:del')")
    public ResponseEntity<Object> delete(@RequestBody final Integer[] ids) {
        this.quartzJobService.removeByIds(new ArrayList<Integer>(Arrays.asList(ids)));
        return new ResponseEntity(HttpStatus.OK);
    }
    
    static {
        log = LoggerFactory.getLogger((Class)QuartzJobController.class);
    }
}
